// pages/consult/consult.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList:['财务咨询','法务咨询'],
    activeIndex:0,
    Finance:[],
    Legal:[],
    pageSize:1,
    pageNum:6,
    keyValue:'',
    isPull:true,
    status:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  var pageSize = this.data.pageSize
  var key = this.data.keyValue
  var status = this.data.status
    this.getFinance(pageSize,key,status)
    console.log('加载')
  },
  search_value:function(e){
    var key = e.detail.send_value
    this.setData({
      keyValue:key,
      status:'search',
      pageSize:1,
      isPull:true
    })
    var page = this.data.pageSize
    var type = this.data.activeIndex
    var status = this.data.status
    if(type==0){
      this.getFinance(page,key,status)
      console.log('搜索')
    }else{
      this.getLegal(page,key,status)
    }
   },
   onLoadFun:function(s){
     console.log(s)
   },
  getFinance:function(page,key,type){
    var that = this
    var data = {
      page:page,
      limit:this.data.pageNum,
      type:1,
      search:key
     }
    request.get('consultation',data,{ noAuth : true}).then(res=>{ 
      console.log(type)
      if(type=='search' || type=='againShow' || type=='switch'){
        that.setData({
          Finance:[]
        })
      }
      if(res.data.length>0){
        that.setData({
          Finance:that.data.Finance.concat(res.data)
        })
     
      }else{
        that.setData({
          isPull:false
        })
      }
    })
  },
  getLegal:function(page,key,type){
    var that = this
    var data = {
      page:page,
      limit:this.data.pageNum,
      type:2,
      search:key
     }
    request.get('consultation',data,{ noAuth : true}).then(res=>{ 
      if(type=='search' || type=='againShow' || type=='switch'){
        that.setData({
          Legal:[]
        })
      }
      if(res.data.length>0){
        that.setData({
          Legal:that.data.Legal.concat(res.data)
        })
      }else{
        that.setData({
          isPull:false
        })
      }
    })
  },

  getIndex:function(e){
    console.log('切换')
    var index = e.detail.index
    this.setData({
      pageSize:1,
      activeIndex:index,
       keyValue:'',
       isPull:true,
       status:'switch'
    })
   var page = this.data.pageSize
   var key = this.data.keyValue
   var type = this.data.activeIndex
    var status = this.data.status
    if(type==0){
      this.getFinance(page,key,status)
    }else{
      this.getLegal(page,key,status)
    }
   
  },

  goMess(){
    var that = this
    var type = that.data.activeIndex
    var id = ++type
    wx.getSetting({
      success(res){
       if(res.authSetting['scope.userInfo']){
       var u =  wx.getStorageSync('userInfo')
        wx.navigateTo({
          url: './message/message?type='+id+'&name='+u.nickname
        })
       }else{
         wx.showModal({
           content: '请前往【个人中心】登录授权',
           success(res){
             if(res.confirm){
               setTimeout(() => {
                   wx.switchTab({
                        url: '../user/user',
                      })
               }, 500);
             }
           }
         })
       }
      }
    })
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('返回')
    this.setData({
      pageSize:1,
      status:'againShow',
      isPull:true
    })
    var page = this.data.pageSize
    var type = this.data.activeIndex
    var status = this.data.status
    if(type==0){
      this.getFinance(page,'',status)
    }else{
      this.setData({
        Legal:[]
      })
      this.getLegal(page,'',status)
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var v = this.data
    this.setData({
      status:''
    })
    if(v.isPull){
     
      var nextPage = ++v.pageSize
      var key=v.keyValue
      var type = this.data.activeIndex
      var status = this.data.status
      if(type==0){
        this.getFinance(nextPage,key,status)
        console.log('下拉')
      }else{
        this.getLegal(nextPage,key,status)
      }
    }
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})